// 核心颜色定义
const CORE_COLORS = {
  purple: {
    50: '#FBFAFF',
    100: '#F5F2FF',
    200: '#E6DFFF',
    300: '#C5B6FC',
    400: '#B19FE6',
    500: '#A68AF9',
    600: '#9580FF',
    700: '#8A5CF5',
    800: '#6B4CA8',
    900: '#483772',
    950: '#2a2c3e',
  },
  neutral: {
    50: '#ffffff',
    100: '#f5f5f5',
    200: '#e0e0e0',
    300: '#dadada',
    400: '#9e9e9e',
    500: '#666666',
    600: '#424242',
    700: '#3a3548',
    800: '#2a2c3e',
    900: '#1d1f21',
    950: '#1a1b1e',
  },
  functional: {
    error: '#ff6b6b',
    errorDark: '#4a2c2c',
    success: '#51cf66',
    successDark: '#2c4a32',
    warning: '#ffd43b',
    warningDark: '#4a3c2c',
    info: '#339af0',
    infoDark: '#2c3a4a',
  },
};

/**
 * 生成 Mermaid 完整主题配置
 * @param {boolean} isDark - 是否为暗色主题
 * @param {string} diagramType - 图表类型
 * @returns {Object} Mermaid 配置对象
 */
export function generateMermaidConfig(isDark, diagramType) {
  // 基础主题变量
  const baseThemeVariables = isDark
    ? {
        // 暗色主题 - 基础颜色
        primaryColor: CORE_COLORS.purple[900],
        primaryTextColor: CORE_COLORS.neutral[300],
        primaryBorderColor: CORE_COLORS.purple[300],
        secondaryColor: CORE_COLORS.purple[700],
        secondaryTextColor: CORE_COLORS.neutral[200],
        secondaryBorderColor: CORE_COLORS.purple[500],
        tertiaryColor: CORE_COLORS.purple[950],
        tertiaryTextColor: CORE_COLORS.neutral[100],
        tertiaryBorderColor: CORE_COLORS.purple[600],

        // 背景和线条
        lineColor: CORE_COLORS.purple[300],
        edgeLabelBackground: CORE_COLORS.neutral[900],
        labelBackground: CORE_COLORS.neutral[900],
        clusterBkg: 'transparent',
        clusterBorder: CORE_COLORS.purple[500],
        defaultLinkColor: CORE_COLORS.purple[300],

        // 注释
        noteTextColor: CORE_COLORS.neutral[300],
        noteBkgColor: CORE_COLORS.purple[950],
        noteBorderColor: CORE_COLORS.purple[700],

        // 文本
        textColor: CORE_COLORS.neutral[300],
        labelTextColor: CORE_COLORS.neutral[300],
        titleColor: CORE_COLORS.neutral[200],

        // 节点
        nodeBkg: CORE_COLORS.purple[900],
        nodeBorder: CORE_COLORS.purple[300],
        mainBkg: CORE_COLORS.purple[900],

        // 特殊节点
        specialNodeBkg: CORE_COLORS.purple[700],
        specialNodeBorder: CORE_COLORS.purple[300],

        // 流程图特定
        flowchartBackground: CORE_COLORS.purple[900],

        // 序列图特定
        actorBkg: CORE_COLORS.purple[900],
        actorBorder: CORE_COLORS.purple[300],
        actorTextColor: CORE_COLORS.neutral[300],
        actorLineColor: CORE_COLORS.purple[600],
        signalColor: CORE_COLORS.purple[300],
        signalTextColor: CORE_COLORS.neutral[300],
        labelBoxBkgColor: CORE_COLORS.purple[950],
        labelBoxBorderColor: CORE_COLORS.purple[700],
        loopTextColor: CORE_COLORS.neutral[300],
        activationBorderColor: CORE_COLORS.purple[300],
        activationBkgColor: CORE_COLORS.purple[900],
        sequenceNumberColor: CORE_COLORS.neutral[50],

        // Alt/Opt/Loop 区块
        altSectionBkgColor: 'rgba(72, 55, 114, 0.2)',
        altSectionBorderColor: CORE_COLORS.purple[500],

        // 类图特定
        classBackground: CORE_COLORS.purple[900],
        classBorder: CORE_COLORS.purple[300],
        classTextColor: CORE_COLORS.neutral[300],
        relationshipLineColor: CORE_COLORS.purple[300],
        relationshipLabelBackground: CORE_COLORS.neutral[900],
        relationshipLabelColor: CORE_COLORS.neutral[300],

        // 甘特图特定
        gridLineColor: CORE_COLORS.neutral[700],
        taskBkgColor: CORE_COLORS.purple[900],
        taskBorderColor: CORE_COLORS.purple[300],
        taskTextColor: CORE_COLORS.neutral[300],
        taskTextOutsideColor: CORE_COLORS.neutral[300],
        doneTaskBkgColor: CORE_COLORS.purple[700],
        doneTaskBorderColor: CORE_COLORS.purple[300],
        activeTaskBkgColor: CORE_COLORS.purple[500],
        activeTaskBorderColor: CORE_COLORS.purple[300],
        futureTaskBkgColor: CORE_COLORS.purple[950],
        futureTaskBorderColor: CORE_COLORS.purple[600],
        critBorderColor: CORE_COLORS.functional.error,
        critBkgColor: CORE_COLORS.functional.errorDark,
        todayLineColor: CORE_COLORS.functional.error,

        // 状态图特定
        stateBkg: CORE_COLORS.purple[900],
        stateBorder: CORE_COLORS.purple[300],
        stateTextColor: CORE_COLORS.neutral[100],
        compositeBkg: CORE_COLORS.purple[950],
        compositeBorder: CORE_COLORS.purple[500],
        compositeTextColor: CORE_COLORS.neutral[100],
        startColor: CORE_COLORS.purple[700],
        endColor: CORE_COLORS.purple[700],
        transitionColor: CORE_COLORS.purple[300],
        transitionLabelColor: CORE_COLORS.neutral[100],
        stateLabelColor: CORE_COLORS.neutral[100],
        stateNodeTextColor: CORE_COLORS.neutral[100],

        // ER图特定
        entityBkg: CORE_COLORS.purple[900],
        entityBorder: CORE_COLORS.purple[300],
        entityTextColor: CORE_COLORS.neutral[300],
        attributeBackgroundColor: CORE_COLORS.purple[950],
        attributeBorderColor: CORE_COLORS.purple[700],
        attributeTextColor: CORE_COLORS.neutral[300],

        // Git图特定
        commitColor: CORE_COLORS.purple[700],
        commitBorderColor: CORE_COLORS.purple[300],
        commitTextColor: CORE_COLORS.neutral[300],
        tagColor: CORE_COLORS.purple[900],
        tagBorderColor: CORE_COLORS.purple[300],
        tagTextColor: CORE_COLORS.neutral[300],

        // 分支颜色
        branch0: CORE_COLORS.purple[700],
        branch1: CORE_COLORS.purple[500],
        branch2: CORE_COLORS.purple[300],
        branch3: CORE_COLORS.purple[600],
        branch4: CORE_COLORS.purple[800],

        // 字体设置
        fontSize: '16px',
        fontFamily: 'ui-sans-serif, system-ui, -apple-system, sans-serif',
      }
    : {
        // 亮色主题 - 基础颜色
        primaryColor: CORE_COLORS.purple[50],
        primaryTextColor: CORE_COLORS.neutral[900],
        primaryBorderColor: CORE_COLORS.purple[700],
        secondaryColor: CORE_COLORS.purple[300],
        secondaryTextColor: CORE_COLORS.neutral[700],
        secondaryBorderColor: CORE_COLORS.purple[500],
        tertiaryColor: CORE_COLORS.purple[100],
        tertiaryTextColor: CORE_COLORS.neutral[600],
        tertiaryBorderColor: CORE_COLORS.purple[400],

        // 背景和线条
        lineColor: CORE_COLORS.purple[700],
        edgeLabelBackground: CORE_COLORS.neutral[50],
        labelBackground: CORE_COLORS.neutral[50],
        clusterBkg: 'transparent',
        clusterBorder: CORE_COLORS.purple[900],
        defaultLinkColor: CORE_COLORS.purple[700],

        // 注释
        noteTextColor: CORE_COLORS.neutral[900],
        noteBkgColor: CORE_COLORS.purple[100],
        noteBorderColor: CORE_COLORS.purple[700],

        // 文本
        textColor: CORE_COLORS.neutral[900],
        labelTextColor: CORE_COLORS.neutral[900],
        titleColor: CORE_COLORS.neutral[800],

        // 节点
        nodeBkg: CORE_COLORS.purple[50],
        nodeBorder: CORE_COLORS.purple[700],
        mainBkg: CORE_COLORS.purple[50],

        // 特殊节点
        specialNodeBkg: CORE_COLORS.purple[300],
        specialNodeBorder: CORE_COLORS.purple[700],

        // 流程图特定
        flowchartBackground: CORE_COLORS.purple[50],

        // 序列图特定
        actorBkg: CORE_COLORS.purple[50],
        actorBorder: CORE_COLORS.purple[700],
        actorTextColor: CORE_COLORS.neutral[900],
        actorLineColor: CORE_COLORS.purple[600],
        signalColor: CORE_COLORS.purple[700],
        signalTextColor: CORE_COLORS.neutral[900],
        labelBoxBkgColor: CORE_COLORS.purple[100],
        labelBoxBorderColor: CORE_COLORS.purple[700],
        loopTextColor: CORE_COLORS.neutral[900],
        activationBorderColor: CORE_COLORS.purple[700],
        activationBkgColor: CORE_COLORS.purple[50],
        sequenceNumberColor: CORE_COLORS.neutral[900],

        // Alt/Opt/Loop 区块
        altSectionBkgColor: 'rgba(197, 182, 252, 0.15)',
        altSectionBorderColor: CORE_COLORS.purple[700],

        // 类图特定
        classBackground: CORE_COLORS.purple[50],
        classBorder: CORE_COLORS.purple[700],
        classTextColor: CORE_COLORS.neutral[900],
        relationshipLineColor: CORE_COLORS.purple[700],
        relationshipLabelBackground: CORE_COLORS.neutral[50],
        relationshipLabelColor: CORE_COLORS.neutral[900],

        // 甘特图特定
        gridLineColor: CORE_COLORS.neutral[200],
        taskBkgColor: CORE_COLORS.purple[50],
        taskBorderColor: CORE_COLORS.purple[700],
        taskTextColor: CORE_COLORS.neutral[900],
        taskTextOutsideColor: CORE_COLORS.neutral[900],
        doneTaskBkgColor: CORE_COLORS.purple[300],
        doneTaskBorderColor: CORE_COLORS.purple[700],
        activeTaskBkgColor: CORE_COLORS.purple[500],
        activeTaskBorderColor: CORE_COLORS.purple[700],
        futureTaskBkgColor: CORE_COLORS.purple[100],
        futureTaskBorderColor: CORE_COLORS.purple[400],
        critBorderColor: CORE_COLORS.functional.error,
        critBkgColor: '#ffeeee',
        todayLineColor: CORE_COLORS.functional.error,

        // 状态图特定
        stateBkg: CORE_COLORS.purple[50],
        stateBorder: CORE_COLORS.purple[700],
        stateTextColor: CORE_COLORS.neutral[900],
        compositeBkg: CORE_COLORS.purple[100],
        compositeBorder: CORE_COLORS.purple[700],
        compositeTextColor: CORE_COLORS.neutral[900],
        startColor: CORE_COLORS.purple[700],
        endColor: CORE_COLORS.purple[700],
        transitionColor: CORE_COLORS.purple[700],
        transitionLabelColor: CORE_COLORS.neutral[900],

        // ER图特定
        entityBkg: CORE_COLORS.purple[50],
        entityBorder: CORE_COLORS.purple[700],
        entityTextColor: CORE_COLORS.neutral[900],
        attributeBackgroundColor: CORE_COLORS.purple[100],
        attributeBorderColor: CORE_COLORS.purple[700],
        attributeTextColor: CORE_COLORS.neutral[900],

        // Git图特定
        commitColor: CORE_COLORS.purple[700],
        commitBorderColor: CORE_COLORS.purple[700],
        commitTextColor: CORE_COLORS.neutral[900],
        tagColor: CORE_COLORS.purple[50],
        tagBorderColor: CORE_COLORS.purple[700],
        tagTextColor: CORE_COLORS.neutral[900],

        // 分支颜色
        branch0: CORE_COLORS.purple[700],
        branch1: CORE_COLORS.purple[500],
        branch2: CORE_COLORS.purple[300],
        branch3: CORE_COLORS.purple[600],
        branch4: CORE_COLORS.purple[800],

        // 字体设置
        fontSize: '16px',
        fontFamily: 'ui-sans-serif, system-ui, -apple-system, sans-serif',
      };

  // 饼图颜色配置
  const pieColors = isDark
    ? [
        CORE_COLORS.purple[700],
        CORE_COLORS.purple[500],
        CORE_COLORS.purple[300],
        CORE_COLORS.purple[900],
        CORE_COLORS.purple[600],
        CORE_COLORS.purple[400],
        CORE_COLORS.purple[800],
        CORE_COLORS.purple[200],
        CORE_COLORS.purple[950],
        CORE_COLORS.neutral[700],
      ]
    : [
        CORE_COLORS.purple[700],
        CORE_COLORS.purple[500],
        CORE_COLORS.purple[300],
        CORE_COLORS.purple[600],
        CORE_COLORS.purple[400],
        CORE_COLORS.purple[800],
        CORE_COLORS.purple[200],
        CORE_COLORS.purple[100],
        CORE_COLORS.purple[900],
        CORE_COLORS.neutral[300],
      ];

  // 基础配置
  const baseConfig = {
    startOnLoad: false,
    fontFamily: 'ui-sans-serif, system-ui, -apple-system, sans-serif',
    theme: isDark ? 'dark' : 'default',
    themeVariables: {
      ...baseThemeVariables,
      // 饼图颜色
      pie1: pieColors[0],
      pie2: pieColors[1],
      pie3: pieColors[2],
      pie4: pieColors[3],
      pie5: pieColors[4],
      pie6: pieColors[5],
      pie7: pieColors[6],
      pie8: pieColors[7],
      pie9: pieColors[8],
      pie10: pieColors[9],
      pie11: pieColors[0],
      pie12: pieColors[1],
      pieTitleTextColor: baseThemeVariables.textColor,
      pieSectionTextColor: isDark ? CORE_COLORS.neutral[50] : CORE_COLORS.neutral[900],
      pieLegendTextColor: baseThemeVariables.textColor,
      pieBorderColor: baseThemeVariables.primaryBorderColor,
      pieOuterStrokeWidth: '2px',
    },

    // 流程图配置
    flowchart: {
      useMaxWidth: true,
      htmlLabels: true,
      curve: 'basis',
      rankSpacing: 50,
      nodeSpacing: 50,
      padding: 15,
      defaultRenderer: 'dagre-wrapper',
      // 添加边标签配置
      wrappingWidth: 200,
      edgeLabelBackground: isDark ? CORE_COLORS.neutral[900] : CORE_COLORS.neutral[50],
      nodeTextColor: isDark ? CORE_COLORS.neutral[300] : CORE_COLORS.neutral[900],
      // 调整标签最小高度
      labelHeight: 30,
      labelPadding: 5,
    },

    // 序列图配置
    sequence: {
      actorMargin: 50,
      boxMargin: 10,
      boxTextMargin: 5,
      noteMargin: 10,
      messageMargin: 35,
      mirrorActors: true,
      useMaxWidth: true,
      rightAngles: false,
      showSequenceNumbers: false,
    },

    // 状态图配置
    state: {
      dividerMargin: 10,
      sizeUnit: diagramType === 'state-v2' ? 3 : 4,
      padding: 5,
      textHeight: 8,
      titleShift: -15,
      noteMargin: 8,
      forkWidth: 60,
      forkHeight: 6,
      miniPadding: 2,
      fontSizeFactor: 5.02,
      fontSize: diagramType === 'state-v2' ? 12 : 14,
      labelHeight: 14,
      edgeLengthFactor: '15',
      compositTitleSize: 30,
      radius: 4,
      ...(diagramType === 'state-v2'
        ? {
            useMaxWidth: false,
            nodeSpacing: 20,
            rankSpacing: 20,
            defaultRenderer: 'dagre-wrapper',
          }
        : {
            useMaxWidth: true,
            nodeSpacing: 25,
            rankSpacing: 25,
          }),
    },

    // 甘特图配置
    gantt: {
      fontSize: 14,
      barHeight: 25,
      gridLineStartPadding: 300,
      useMaxWidth: true,
      numberSectionStyles: 4,
      axisFormat: '%Y-%m-%d',
      topAxis: false,
    },

    // ER图配置
    er: {
      fontSize: 14,
      useMaxWidth: true,
      minEntityWidth: 100,
      minEntityHeight: 75,
      entityPadding: 15,
      stroke: baseThemeVariables.lineColor,
      fill: baseThemeVariables.mainBkg,
    },

    // 饼图配置
    pie: {
      useMaxWidth: true,
      textPosition: 0.75,
    },

    // 类图配置
    class: {
      useMaxWidth: true,
      defaultRenderer: 'dagre-wrapper',
    },

    // Git图配置
    gitGraph: {
      mainBranchName: 'main',
      showCommitLabel: true,
      showBranches: true,
      rotateCommitLabel: false,
    },

    // 用户旅程图配置
    journey: {
      useMaxWidth: true,
      diagramMarginX: 50,
      diagramMarginY: 10,
      actorMargin: 50,
      width: 150,
      height: 50,
      boxMargin: 10,
      boxTextMargin: 5,
      noteMargin: 10,
      messageMargin: 35,
      messageAlign: 'center',
      bottomMarginAdj: 1,
      rightAngles: false,
      taskFontSize: 14,
      taskFontFamily: 'ui-sans-serif, system-ui, -apple-system, sans-serif',
      taskMargin: 50,
      activationWidth: 10,
      textPlacement: 'fo',
      actorColours: pieColors,
      sectionFills: pieColors.slice(0, 7),
      sectionColours: pieColors.slice(0, 7),
    },
  };

  // 对状态图的特殊主题变量
  if (diagramType === 'state-v2' || diagramType === 'state') {
    baseConfig.themeVariables.stateFontSize = '11px';
    baseConfig.themeVariables.stateLabelFontSize = '10px';
    baseConfig.themeVariables.stateNodePadding = '2px';
    baseConfig.themeVariables.compositNodePadding = '2px';
  }

  // 添加流程图特定的主题变量
  if (diagramType === 'flow') {
    baseConfig.themeVariables.edgeLabelBackgroundColor = isDark
      ? CORE_COLORS.neutral[900]
      : CORE_COLORS.neutral[50];
    baseConfig.themeVariables.edgeLabelColor = isDark
      ? CORE_COLORS.neutral[300]
      : CORE_COLORS.neutral[900];
    // 设置最小高度
    baseConfig.themeVariables.edgeLabelHeight = '30px';
  }

  return baseConfig;
}

export { CORE_COLORS };
